package cn.ante.pet.controller;

import cn.ante.basic.exception.BusinessException;
import cn.ante.basic.jwt.UserInfo;
import cn.ante.basic.utils.LoginContext;
import cn.ante.org.domain.Employee;
import cn.ante.org.service.IEmployeeService;
import cn.ante.pet.dto.AcceptDto;
import cn.ante.pet.service.ISearchMasterMsgService;
import cn.ante.pet.domain.SearchMasterMsg;
import cn.ante.pet.query.SearchMasterMsgQuery;
import cn.ante.basic.utils.AjaxResult;
import cn.ante.basic.utils.PageList;
import cn.ante.user.domain.Logininfo;
import cn.ante.user.domain.User;
import cn.ante.user.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/searchMasterMsg")
public class SearchMasterMsgController {
    @Autowired
    public ISearchMasterMsgService searchMasterMsgService;

    @Autowired
    private IUserService userService;

    @Autowired
    private IEmployeeService employeeService;

    /**
     * 保存和修改公用的
     *
     * @param searchMasterMsg 传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody SearchMasterMsg searchMasterMsg) {
        try {
            if (searchMasterMsg.getId() != null)
                searchMasterMsgService.update(searchMasterMsg);
            else
                searchMasterMsgService.add(searchMasterMsg);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存对象失败！" + e.getMessage());
        }
    }

    /**
     * 删除对象信息
     *
     * @param id
     * @return
     */
    @DeleteMapping(value = "/{id}")
    public AjaxResult delete(@PathVariable("id") Long id) {
        try {
            searchMasterMsgService.remove(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除对象失败！" + e.getMessage());
        }
    }

    //获取用户
    @GetMapping("/{id}")
    public SearchMasterMsg get(@PathVariable("id") Long id) {
        return searchMasterMsgService.loadById(id);
    }


    /**
     * 查看所有的员工信息
     *
     * @return
     */
    @GetMapping()
    public List<SearchMasterMsg> list() {

        return searchMasterMsgService.loadAll();
    }


    /**
     * 分页查询数据
     *
     * @param query 查询对象
     * @return PageList 分页对象
     */
    @PostMapping("/list")
    public PageList<SearchMasterMsg> json(@RequestBody SearchMasterMsgQuery query) {
        return searchMasterMsgService.findAll(query);
    }

    /**
    * @Description: 发布寻主信息
    * @Author: Nai
    * @Version: 1.0
    * @Date:  2022/7/20 19:35
    */
    @PostMapping("/publish")
    public AjaxResult publish(@RequestBody SearchMasterMsg searchMasterMsg, HttpServletRequest request) {
        try {
            // 查找到当前登陆人,获取到登陆用户信息
            Logininfo logininfo = LoginContext.getLoginifo(request);
            User user = userService.loadByLogininfoId(logininfo.getId());
            // 将寻主信息中的useid绑定上
            searchMasterMsg.setUserId(user.getId());
            searchMasterMsgService.publish(searchMasterMsg);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除对象失败！" + e.getMessage());
        }
    }

    /**
     * @Description: 寻主信息发布后的待审核界面，被驳回的审核也是待审核界面，此时平台管理员可以查看到此消息
     * @Author: Nai
     * @Version: 1.0
     * @Date: 2022/7/20 13:07
     */
    @PostMapping("/toAudit")
    public PageList<SearchMasterMsg>  toAudit(@RequestBody SearchMasterMsgQuery query) {
        // 设置寻主状态为待审核
        query.setState(0);
        return searchMasterMsgService.findAll(query);
    }

    /**
     * @Description: 寻主消息在寻主池时的状态，shopid为null,且寻主消息的状态为审核通过，此时店铺管理员能看到
     * @Author: Nai
     * @Version: 1.0
     * @Date: 2022/7/20 13:18
     */
    @PostMapping("/pool")
    public PageList<SearchMasterMsg>  pool(@RequestBody SearchMasterMsgQuery query) {
        // 设置寻主状态为审核通过，但是shopid为null
        query.setState(1);
        return searchMasterMsgService.findAll(query);
    }

    /**
     * @Description: 寻主消息已被店铺接单但是待处理状态，shopid为当前登录者店铺管理员的shopid,
     *  且寻主消息的状态为审核通过，此时店铺管理员能看到，且寻主信息的id也被使用
     * @Author: Nai
     * @Version: 1.0
     * @Date: 2022/7/20 13:18
     */
    @PostMapping("/toHandle")
    public PageList<SearchMasterMsg>  toHandle(@RequestBody SearchMasterMsgQuery query, HttpServletRequest request) {
        // 设置寻主状态为审核通过
        query.setState(1);
        // 根据当前登录者查询到员工，然后将寻主消息的shopid绑定上该员工的shopid
        Logininfo loginifo = LoginContext.getLoginifo(request);
        Employee employee = employeeService.loadByLoginifoId(loginifo.getId());
        query.setShopId(employee.getShopId());
        return searchMasterMsgService.findAll(query);
    }

    /**
    * @Description: 寻主消息已经完成了寻主，店铺已经有人接单，平台管理员和店铺管理员都能看到
    * @Author: Nai
    * @Version: 1.0
    * @Date:  2022/7/20 14:00
    */
    @PostMapping("/finish")
    public PageList<SearchMasterMsg>  finish(@RequestBody SearchMasterMsgQuery query, HttpServletRequest request) {
        // 设置寻主状态为已完成
        query.setState(2);
        return searchMasterMsgService.findAll(query);
    }

    /**
    * @Description: 前台用户发布寻主消息看到的页面
    * @Author: Nai
    * @Version: 1.0
    * @Date:  2022/7/20 14:03
    */
//    @PostMapping("/user")
//    public PageList<SearchMasterMsg>  user(@RequestBody SearchMasterMsgQuery query, HttpServletRequest request) {
//        Logininfo loginifo = LoginContext.getLoginifo(request);
//        User user = userService.loadByLogininfoId(loginifo.getId());
//        List<SearchMasterMsg> msgs = searchMasterMsgService.loadByUserId(user.getId());
//        return searchMasterMsgService.findAll(query);
//    }

    @PostMapping("/accept")
    public AjaxResult acceptOrders(@RequestBody AcceptDto acceptDto) {
        try {
            searchMasterMsgService.acceptOrders(acceptDto);
            return AjaxResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除对象失败！");
        }
    }

    @PostMapping("/reject/{id}")
    public AjaxResult rejectOrders(@PathVariable("id") Long id) {
        try {
            searchMasterMsgService.rejectOrders(id);
            return AjaxResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除对象失败！");
        }
    }
}
